package com.example.sprotsapi.Controller;

import com.example.sprotsapi.Pojo.user;
import com.example.sprotsapi.Service.userService;
import com.example.sprotsapi.Utils.JwtUtil;
import com.example.sprotsapi.Utils.Result;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class loginController {

    @Autowired
    userService userService;

    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody user user){
        user loginUser = userService.getUserPhone(user.getPhone());
        Map<String,Object> map = new HashMap<>();
        if(loginUser == null){
            map.put("code","-1");
            map.put("msg","用户不存在");
            return map;
        }else{
            user isUser = userService.login(user.getPhone(), user.getPassword());
            if(isUser == null){
                map.put("code","-1");
                map.put("msg","密码错误");
                return map;
            }else{
               String token = JwtUtil.createToken(user.getPhone());
                map.put("code","200");
                map.put("msg","登录成功");
                map.put("data",token);
                return map;
            }
        }
    }
}
